Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

thenby

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

thenby

Micro library for sorting arrays using the firstBy().thenBy().thenBy() syntax

  • 1.3.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
451K
decreased by-26.08%
Maintainers
1
Weekly downloads
 
Created

What is thenby?

The 'thenby' npm package is a utility for performing multi-level sorting of arrays. It allows you to sort arrays by multiple criteria in a concise and readable manner.

What are thenby's main functionalities?

Basic Multi-Level Sorting

This feature allows you to sort an array of objects first by the 'name' property and then by the 'age' property. The 'thenBy' method is chained to add additional sorting criteria.

const firstBy = require('thenby');

const data = [
  { name: 'John', age: 30, city: 'New York' },
  { name: 'Jane', age: 25, city: 'San Francisco' },
  { name: 'John', age: 25, city: 'Los Angeles' },
  { name: 'Jane', age: 30, city: 'Chicago' }
];

const sortedData = data.sort(
  firstBy('name')
  .thenBy('age')
);

console.log(sortedData);

Custom Comparator Functions

This feature allows you to use custom comparator functions for sorting. In this example, the 'name' property is sorted using 'localeCompare' for string comparison, and the 'age' property is sorted using a numerical comparison.

const firstBy = require('thenby');

const data = [
  { name: 'John', age: 30, city: 'New York' },
  { name: 'Jane', age: 25, city: 'San Francisco' },
  { name: 'John', age: 25, city: 'Los Angeles' },
  { name: 'Jane', age: 30, city: 'Chicago' }
];

const sortedData = data.sort(
  firstBy((a, b) => a.name.localeCompare(b.name))
  .thenBy((a, b) => a.age - b.age)
);

console.log(sortedData);

Descending Order Sorting

This feature allows you to sort in descending order by specifying the 'direction' option. In this example, both 'name' and 'age' properties are sorted in descending order.

const firstBy = require('thenby');

const data = [
  { name: 'John', age: 30, city: 'New York' },
  { name: 'Jane', age: 25, city: 'San Francisco' },
  { name: 'John', age: 25, city: 'Los Angeles' },
  { name: 'Jane', age: 30, city: 'Chicago' }
];

const sortedData = data.sort(
  firstBy('name', { direction: -1 })
  .thenBy('age', { direction: -1 })
);

console.log(sortedData);

Other packages similar to thenby

Keywords

FAQs

Package last updated on 04 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc